package com.isdepci.project.factor.service.impl;

import java.util.List;

import com.isdepci.common.utils.StringUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.factor.mapper.CategorizeInfoMapper;
import com.isdepci.project.factor.domain.CategorizeInfo;
import com.isdepci.project.factor.service.ICategorizeInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分类管理Service业务层处理
 *
 * @author isdepci
 * @date 2024-10-09
 */
@Service
public class CategorizeInfoServiceImpl implements ICategorizeInfoService {

    @Autowired
    private CategorizeInfoMapper categorizeInfoMapper;

    /**
     * 查询分类管理
     */
    @Override
    public CategorizeInfo selectCategorizeInfoById(Long id) {
        return categorizeInfoMapper.selectCategorizeInfoById(id);
    }

    /**
     * 查询分类管理列表
     */
    @Override
    public List<CategorizeInfo> selectCategorizeInfoList(CategorizeInfo categorizeInfo) {
        return categorizeInfoMapper.selectCategorizeInfoList(categorizeInfo);
    }

    /**
     * 新增分类管理
     */
    @Override
    public AjaxResult insertCategorizeInfo(CategorizeInfo categorizeInfo) {
        if(categorizeInfo.getParentId()!=0){
            CategorizeInfo info = categorizeInfoMapper.selectCategorizeInfoById(categorizeInfo.getParentId());
            if (info.getStatus() != 1) {
                return AjaxResult.error("当前分类停用，不允许新增");
            }
            categorizeInfo.setAncestors(info.getAncestors() + "," + categorizeInfo.getParentId());
        }else {
            categorizeInfo.setAncestors("0");
        }
        categorizeInfo.setCreateUser();
        int row = categorizeInfoMapper.insertCategorizeInfo(categorizeInfo);
        if (row > 0) {
            FactorRedisUtils.clearListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改分类管理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateCategorizeInfo(CategorizeInfo categorize) {
        CategorizeInfo newParent = categorizeInfoMapper.selectCategorizeInfoById(categorize.getParentId());
        CategorizeInfo old = categorizeInfoMapper.selectCategorizeInfoById(categorize.getId());
        if (StringUtils.isNotNull(newParent) && StringUtils.isNotNull(old)) {
            String newAncestors = newParent.getAncestors() + "," + newParent.getId();
            String oldAncestors = old.getAncestors();
            categorize.setAncestors(newAncestors);
            updateChildren(categorize.getId(), newAncestors, oldAncestors);
        }
        categorize.setUpdateUser();
        int row = categorizeInfoMapper.updateCategorizeInfo(categorize);
        if (categorize.getStatus() == 1) {
            updateParentStatus(categorize);
        }
        if(old.getStatus()==1&&categorize.getStatus()!=1){
            categorizeInfoMapper.updateChildrenStatus(categorize.getId(),2);
        }
        if (row > 0) {
            FactorRedisUtils.clearListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    public void updateChildren(Long id, String newAncestors, String oldAncestors) {
        List<CategorizeInfo> children = categorizeInfoMapper.selectChildrenById(id);
        for (CategorizeInfo child : children) {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            categorizeInfoMapper.updateChildren(children);
        }
    }

    private void updateParentStatus(CategorizeInfo info){
        String updateBy = info.getUpdateBy();
        info = categorizeInfoMapper.selectCategorizeInfoById(info.getId());
        info.setUpdateBy(updateBy);
        categorizeInfoMapper.updateParentStatus(info);
    }

    /**
     * 批量删除分类管理
     */
    @Override
    public AjaxResult deleteCategorizeInfoById(Long id) {
        List<CategorizeInfo> children = categorizeInfoMapper.selectChildrenById(id);
        if (children.size()>0) {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        int row = categorizeInfoMapper.deleteCategorizeInfoById(id);
        if (row > 0) {
            FactorRedisUtils.clearListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
